﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Space_Conquest
{
    class PowerPlant : BuildingUnit 
    {

        protected bool _isProducingElectricity;


        protected int _electricityRatio;
        protected int _fuelRatio;

        protected int _maxElectricityRatio;
        protected int _maxFuelRatio;

        public bool IsProducingElectricity
        {
            get
            {
                return _isProducingElectricity;
            }
            set
            {
                _isProducingElectricity = value;
            }
        }

        public static Texture _texturePowerPlant = new Texture("Images/Building/PowerPlant.bmp");
        public override int CreationPercentFinished
        {
            get
            {
                if (UnitTypeCurrentlyCreating == BuildingCreationQueueType.Laborer)
                {
                    return (int)(((double)_currentCreationTime / 300.0) * 100);
                }
                else
                {
                    return 0;
                }
            }
        }

        public override int BuiltPercentFinished
        {
            get
            {
                return (int)(((double)_currentConstructionTime / 1000.0) * 100);
            }
        }

        public PowerPlant(int row, int column, Player owner)
            : base(_texturePowerPlant, row, column, owner)
        {
            _hitPoints = _maxHitPoints = 600;

            _necessaryBuildTime = 500;

            _area.Height = 2;
            _area.Width = 2;
            _layer = UnitLayer.Ground;
            _visionRange = 5;

            SetLocation();

            _isProducingElectricity = false;

            _fuelRatio = 0;
            _electricityRatio = 0;

            _maxFuelRatio = 75;
            _maxElectricityRatio = 85;

        }

        public override void Motion()
        {

            // Anything to create?
            if (IsCreatingUnit)
            {

                if (UnitTypeCurrentlyCreating == BuildingCreationQueueType.Laborer)
                {

                    if (_currentCreationTime >= 300)
                    {

                        // Attemp to place unit around building
                        List<MapPoint> possibleDropLocations = _area.GetListOfAdjacentMapPoints();

                        foreach (MapPoint point in possibleDropLocations)
                        {

                            if (Parent.Parent.Map.TileIsPassable(UnitLayer.Ground, point.X, point.Y))
                            {

                                // Remove from queue and reset timer
                                _currentCreationTime = 0;
                                _creationQueue.RemoveAt(0);

                                // Place unit on open spot
                                Parent.Parent.UnitManager.AddUnit(new Laborer(point.X, point.Y, this.Owner));
                                break;
                            }

                        }

                    }
                    else
                    {
                        _currentCreationTime++;
                    }

                }


            }

            if (_isProducingElectricity && _owner.Fuel > 0 && _isBuilt)
            {

                ++_fuelRatio;
                ++_electricityRatio;

                if (_fuelRatio >= _maxFuelRatio)
                {
                    _owner.Fuel--;
                    _fuelRatio = 0;
                }

                if (_electricityRatio >= _maxElectricityRatio)
                {
                    _owner.Electricity++;
                    _electricityRatio = 0;
                }

            }

        }
    }
}
